/*****************************************************************************************
**  Author:jxx 2022
**  QQ:283591387
**完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
**常用示例见：http://v2.volcore.xyz/document/vueDev
**后台操作见：http://v2.volcore.xyz/document/netCoreDev
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import pop_common from '@/extension/system/float/pop_common.vue'//
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader: '',
    gridBody: pop_common,
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  text:"需求车型物料号标红表示未从SAP同步到BOM",
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    //下面这些方法可以保留也可以删除
    onInit() {  //框架初始化配置前，
      //示例：在按钮的最前面添加一个按钮
      //   this.buttons.unshift({  //也可以用push或者splice方法来修改buttons数组
      //     name: '按钮', //按钮名称
      //     icon: 'el-icon-document', //按钮图标vue2版本见iview文档icon，vue3版本见element ui文档icon(注意不是element puls文档)
      //     type: 'primary', //按钮样式vue2版本见iview文档button，vue3版本见element ui文档button
      //     onClick: function () {
      //       this.$Message.success('点击了按钮');
      //     }
      //   });
      this.tableHeight = window.innerHeight*0.70
      this.setFiexdSearchForm(true);
      this.singleSearch = null;

      //示例：设置修改新建、编辑弹出框字段标签的长度
      this.boxOptions.labelWidth = 140;
      this.buttons.push({  //也可以用push或者splice方法来修改buttons数组
        name: '审核提交', //按钮名称
        value: "submit",
        icon: 'el-icon-check', //按钮图标vue2版本见iview文档icon，vue3版本见element ui文档icon(注意不是element puls文档)
        type: 'primary', //按钮样式vue2版本见iview文档button，vue3版本见element ui文档button
        onClick: function () {
          let rows = this.getSelectRows();
          if (rows.length == 0) {
            this.$message.error("请选择行！");
            return;
          }
          if (rows.length != 1) {
            return this.$error('只能选择一行数据进行审核提交!');
          }
          let ss = [];
          rows.forEach(e => {
            ss.push(e.cw_djlc_djzb_id);//
          })
          rows.forEach(element => {
            if (element.stepid == 10 && element.is_have_disposition == 0) {
              return this.$error('请先上传配置文件,然后进行审核提交操作');
            }
            this.$store.getters.data().ss_string = ss.join(",")//业务数据id
            this.$store.getters.data().refresh_table = this;
            this.$refs.gridBody.open_pop_submit_show();
          });
        }
      },
      {  //也可以用push或者splice方法来修改buttons数组
        name: '废弃', //按钮名称
        value: "submit",
        icon: 'el-icon-check', //按钮图标vue2版本见iview文档icon，vue3版本见element ui文档icon(注意不是element puls文档)
        type: 'primary', //按钮样式vue2版本见iview文档button，vue3版本见element ui文档button
        onClick: function (result) {
         // let _userInfo = this.$store.getters.getUserInfo();
          let rows = this.getSelectRows();
          if (rows.length == 0) {
            this.$message.error("请选择行！");
            return;
          }
          if (rows.length != 1) {
            return this.$error('只能选择一行数据进行回退!');
          }

          this.$confirm("确认要废弃选择的数据吗?", '警告', {
            confirmButtonText: '确定',
            cancelButtonText: "取消",
            type: 'warning',
            top: false
          }).then(() => { 
              let ht = [];
              rows.forEach(e => {
                ht.push(e.cw_djlc_djzb_id);//
              })
              rows.forEach(element => {
                this.$store.getters.data().ss_string = ht.join(",")//业务数据id
                let turl = "/api/cw_djlc_djzb/updateIsEnable?cw_djlc_djzb_id=" + element.cw_djlc_djzb_id 
                  this.http.post(turl, {}, true).then((r) => {
                  this.refresh();
                })
              });
            })
        }
      },
      {  //也可以用push或者splice方法来修改buttons数组
        name: 'SAPBOM变更', //按钮名称
        value: "submit",
        icon: 'el-icon-check', //按钮图标vue2版本见iview文档icon，vue3版本见element ui文档icon(注意不是element puls文档)
        type: 'primary', //按钮样式vue2版本见iview文档button，vue3版本见element ui文档button
        onClick: function () {
         // let _userInfo = this.$store.getters.getUserInfo();
          let rows = this.getSelectRows();
          if (rows.length == 0) {
            this.$message.error("请选择行！");
            return;
          }
          if (rows.length != 1) {
            return this.$error('只能选择一行数据进行回退!');
          }

          this.$confirm("确认要变更选择的数据吗?", "SAPBOM 变更警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
          }).then(() => {
            let ht = [];
            rows.forEach(e => {
              ht.push(e.cw_djlc_djzb_id);//
            })
            rows.forEach(element => {
              this.$store.getters.data().ss_string = ht.join(",")//业务数据id
              let turl = "/api/cw_djlc_wlzb/updateWlzbBySAPBOM?cw_djlc_djzb_id=" + element.cw_djlc_djzb_id 
                this.http.post(turl, {}, true).then((r) => {
                this.refresh();
              })
            });
        }); 

        }
      }
      
      );
      this.columns.forEach(column => {
        if (column.field == "newmaterialnum") {
          column.cellStyle = (row, rowIndex, columnIndex) => {
            if (row.is_synchronized_bom == '0') {
              return { background: "red", color: "#ffff" }
            }
            
          }
        }
      });

    },
    getOption(field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    onInited() {
      this.columns.forEach(x => {
        x.showOverflowTooltip=true;//内容浮动显示
      });
      //框架初始化配置后
      //如果要配置明细表,在此方法操作
      //this.detailOptions.columns.forEach(column=>{ });
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == 'market_competitive_price') {
            //多文件上传，两个属性要同时用
            item.multiple = true;
            item.maxFile = 5;
          }
          if (item.field == 'new_product_upload') {
            //多文件上传，两个属性要同时用
            item.multiple = true;
            item.maxFile = 5;
          }
        })
      });

      this.buttons.forEach(x => {
        if (x.name == "SAPBOM变更" || x.name == "废弃") {
          let turl = "/api/cw_djlc_djzb/getUserInfo";
          this.http.post(turl, {}, true).then((e) => {
            if(e.role_Id != 1053 && e.role_Id != 1055 && e.role_Id != 1){//财务核准、财务核价、超级管理员
              //隐藏按钮
              x.hidden=true;
            }
          })
        }
      })

      this.columns.unshift({
        field: "detail",
        title: "详细",
        width: 400,
        align: "center",
        render: (h, { row, column, index }) => {
          var temp = "";
          var caigTemp = "";
          var chayiTemp = "";
          var chayiTotalTemp = "";
          var config_temp = "";
          // 这个地方设置标题。如果没有标题页面 不展示
          if(row.stepid == 20 && row.is_have_bom == 0){
            temp = "对标物料号BOM";
          }
          if(row.stepid == 30){
            caigTemp = "需求车型物料号";
          }
          if(row.stepid == 40 || row.stepid == 50 || row.stepid == 60){
            chayiTemp = "差异分析";
            chayiTotalTemp = "差异分析汇总";
          }
    
          if(row.configuration_type==1 || row.configuration_type==2 || row.configuration_type==3){
            config_temp = "配置表";
          }
          //下面所有需要显示的信息都从row里面取出来
          return h(
            "div", { style: { color: '#0c83ff', 'font-size': '13px', cursor: 'pointer' } },
            [
              h(
                "i", {
                style: { 'margin-right': '10px' },
                class: ['el-icon-'],
                onClick: (e) => {
                  e.stopPropagation();
                  console.log(row);
                  let turl = "/api/fk_reimfloat/get_curr_reimfloat?reim_id=" + row["cw_djlc_djzb_id"]
                  this.http.post(turl, {}, true).then((r) => {
                    //console.log(r);
                    this.$refs.gridBody.open_pop_look_status();
                    this.$store.getters.data().reimid = row["cw_djlc_djzb_id"];
                    this.$nextTick(() => {
                      this.$refs.gridBody.$refs.pop_look_status.$refs.el_step.step_items.splice(0, this.$refs.gridBody.$refs.pop_look_status.$refs.el_step.step_items.length)
                      this.$refs.gridBody.$refs.pop_look_status.$refs.el_step.step_items.push(...r);
                      let ii = r.findIndex((x) => { return x.stepid == row["stepid"] })//返回第一次出现位置
                      this.$refs.gridBody.$refs.pop_look_status.$refs.el_step._active = ii + 1;
                    })
                  })
                }
              }, [], '进度'
              ),
              h(
                "i", {
                style: { 'margin-right': '10px' },
                class: ['el-icon-'],
                onClick: (e) => {
                  e.stopPropagation();
                  this.$refs.gridBody.cw_djlc_wlzb_volbox();
                  // 这个地方给个a的原因是如果创建的对标物料不存在，需要手动导入的时候，在查询的时候就需要给个值。不然bomnumber默认为空就查询所有的值
                  var temp = "a"; 
                  if(row["oldbomnumber"] != null){
                    temp = row["oldbomnumber"] ;
                  }
                  this.$store.getters.data().bomnumber = temp;
                  this.$store.getters.data().cw_djlc_code = row["cw_djlc_code"] 
                }
              }, [], temp
              ),
              h(
                "i", {
                style: { 'margin-right': '10px' },
                class: ['el-icon-'],
                onClick: (e) => {
                  e.stopPropagation();
                  this.$refs.gridBody.cw_djlc_wljgwh_volbox();
                  this.$store.getters.data().bomnumber = row["newbomnumber"];
                }
              }, [], caigTemp
              ),
              
              h(
                "i", {
                style: { 'margin-right': '10px' },
                class: ['el-icon-'],
                onClick: (e) => {
                  e.stopPropagation();
                  this.$store.temp = "cw_djlc_djzb" ; 
                  this.$refs.gridBody.cw_djlc_analyze_volbox();
                  this.$store.getters.data().cw_djlc_code = row["cw_djlc_code"];
                  this.$store.getters.data().groupnum = "";
                }
              }, [], chayiTemp
              ),
              h(
                "i", {
                style: { 'margin-right': '10px' },
                class: ['el-icon-'],
                onClick: (e) => {
                  e.stopPropagation();
                  this.$refs.gridBody.cw_djlc_analyze_total_volbox();
                  this.$store.getters.data().cw_djlc_code = row["cw_djlc_code"];
                }
              }, [], chayiTotalTemp
              ),
              h(
                "i", {
                style: { 'margin-right': '10px' },
                class: ['el-icon-'],
                onClick: (e) => {
                  this.$store.getters.data().cw_djlc_djzb_id = row["cw_djlc_djzb_id"];
                  this.$store.getters.data().cw_djlc_djzb_refresh = this;
                  e.stopPropagation();
                  let type = row["configuration_type"];
                  if(type == 1 || type == 3){ //传统车、国际车
                    this.$refs.gridBody.cw_djlc_configuration_tradition_volbox();
                  }else if(type==2){//新能源车
                    this.$refs.gridBody.cw_djlc_configuration_new_energy_volbox();
                  }
                }
              }, [], config_temp
              )  
            ])
        },
      });

      let configuration_type = this.getOption("configuration_type");
      let salechannel = this.getOption("salechannel");
      configuration_type.onChange = (val, option) => {
          if (val == '1') {
            salechannel.data.forEach((kv) => {
              //根据字典的值判断
              if (kv.value.indexOf('经销') > -1) {
                kv.hidden = false;
              } else {
                kv.hidden = true;
              }
            });
  
          } else if (val == '2') {
            salechannel.data.forEach((kv) => {
              //根据字典的值判断
              if (kv.value.indexOf('新能源') > -1) {
                kv.hidden = false;
              } else {
                kv.hidden = true;
              }
            });
  
          } else if (val == '3') {
            salechannel.data.forEach((kv) => {
              //根据字典的值判断
              if (kv.value.indexOf('国际') > -1) {
                kv.hidden = false;
              } else {
                kv.hidden = true;
              }
            });
          } 
      }

    },
    searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      if(formData.mainData.oldmaterialnum.trim().length > 16){
        this.$message.error("请确认对标物料号是否填写正确");
        return false;
      }
      if(formData.mainData.newmaterialnum.trim().length > 16){
        this.$message.error("请确认需求物料号是否填写正确");
        return false;
      }
      formData.mainData.oldmaterialnum = formData.mainData.oldmaterialnum.trim();
      formData.mainData.newmaterialnum = formData.mainData.newmaterialnum.trim();
      return true;
    },
    addAfter(result){
      if (result.status == true) {
         this.$message.success("创建成功");
      }else{
         this.$message.error(result.message);
      }
      this.refresh();
      this.boxModel=false;
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      if(formData.mainData.oldmaterialnum.trim().length > 16){
        this.$message.error("请确认对标物料号是否填写正确");
        return false;
      }
      if(formData.mainData.newmaterialnum.trim().length > 16){
        this.$message.error("请确认需求物料号是否填写正确");
        return false;
      }
      formData.mainData.oldmaterialnum = formData.mainData.oldmaterialnum.trim();
      formData.mainData.newmaterialnum = formData.mainData.newmaterialnum.trim();
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
    },
    modelOpenAfter(row) {
      //点击编辑、新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
      //(1)判断是编辑还是新建操作： this.currentAction=='Add';
      //(2)给弹出框设置默认值
      //(3)this.editFormFields.字段='xxx';
      //如果需要给下拉框设置默认值，请遍历this.editFormOptions找到字段配置对应data属性的key值
      //看不懂就把输出看：console.log(this.editFormOptions)
      //下拉框级联
      // var oldbomnumber = this.getOption("oldbomnumber");
      // var oldmaterialnum = this.getOption("oldmaterialnum");
      this.getOption("oldbomnumber").disabled = true;
      this.getOption("newbomnumber").disabled = true;

      // oldbomnumber.onChange = (val, key) => {
      //   oldmaterialnum.disabled = true;
      //   key.forEach(element => {
      //     if (element.value == val) {
      //       this.editFormFields.oldmaterialnum = element.id;
      //     }
      //   });
      // }
      if (this.currentAction == "Add") {
        this.editFormOptions.forEach(x => {
          x.forEach(item => {
            if (item.field == "oldbomnumber") {
              //如果不是新建，则隐藏Remark,SellNo两个字段是否显示
              //也可以根据row当前编辑行的值来处理
              //vue3版本改为item.hidden=val == this.currentAction != "Add"
              item.hidden = true;
              // this.$set(item, "hidden", this.currentAction != "Add")
            }
            if (item.field == "newbomnumber") {
              //如果不是新建，则隐藏Remark,SellNo两个字段是否显示
              //也可以根据row当前编辑行的值来处理
              //vue3版本改为item.hidden=val == this.currentAction != "Add"
              item.hidden = true;
              // this.$set(item, "hidden", this.currentAction != "Add")
            }
          })
        })
      } else {
        this.editFormOptions.forEach(x => {
          x.forEach(item => {
            if (item.field == "oldbomnumber") {
              //如果不是新建，则隐藏Remark,SellNo两个字段是否显示
              //也可以根据row当前编辑行的值来处理
              //vue3版本改为item.hidden=val == this.currentAction != "Add"
              item.hidden = true;
              // this.$set(item, "hidden", this.currentAction != "Add")
            }
            if (item.field == "newbomnumber") {
              //如果不是新建，则隐藏Remark,SellNo两个字段是否显示
              //也可以根据row当前编辑行的值来处理
              //vue3版本改为item.hidden=val == this.currentAction != "Add"
              item.hidden = true;
              // this.$set(item, "hidden", this.currentAction != "Add")
            }
          })
        })
      }
    }
  }
};
export default extension;
